package com.yanqu.road.dao.impl.activity.douluoshrek;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.shrek.DouLuoShrekEventData;
import com.yanqu.road.entity.activity.shrek.DouLuoShrekStudentData;
import com.yanqu.road.entity.activity.shrek.DouLuoShrekUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DouLuoShrekUserDataDaoImpl extends TempDataObjectDao<DouLuoShrekUserData> {


    public static String getDouLuoShrekUserDataSql = "select * from t_u_activity_douluo_shrek where activityId=? and userId=?";
    public DouLuoShrekUserData getDouLuoShrekUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDouLuoShrekUserDataSql);
            int i = 1;
            call.setInt(i++,activityId);
                call.setLong(i++,userId);
                rs = call.executeQuery();
                while (rs.next()) {
                    DouLuoShrekUserData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertDouLuoShrekUserDataSql =
            "insert into t_u_activity_douluo_shrek(" +
                    "activityId,userId,integral,produceTime,studentCount,buildingLevelData,buildingLevelUpTimeData,skillLevelData,studentData,updateTime,bossId,bossBlood,bossKillTimes,solderData,eventTime,eventData,titleCountData,cacheDisappearStudent,dieCountData" +
                    ") values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(DouLuoShrekUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getIntegral()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getProduceTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStudentCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getBuildingLevelMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getBuildingLevelUpTimeMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getSkillLevelMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getStudentMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBossId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getBossBlood().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBossKillTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getSolderData())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastEventTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getSolderTitleCountMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getCacheDisappearMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getDieCountMap())));

        if (getDb().executeNoneQuery(insertDouLuoShrekUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDouLuoShrekUserDataSql =
            "update t_u_activity_douluo_shrek set " +
                    " integral=?,produceTime=?,studentCount=?,buildingLevelData=?,buildingLevelUpTimeData=?,skillLevelData=?,studentData=?,updateTime=?," +
                    " bossId=?,bossBlood=?,bossKillTimes=?,solderData=?,eventTime=?,eventData=?,titleCountData=?,cacheDisappearStudent=?,dieCountData=?" +
                    " where activityId=? and userId=?";
    @Override
    protected boolean updateData(DouLuoShrekUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getIntegral()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getProduceTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStudentCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getBuildingLevelMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getBuildingLevelUpTimeMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getSkillLevelMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getStudentMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBossId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getBossBlood().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBossKillTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getSolderData())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastEventTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getSolderTitleCountMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getCacheDisappearMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getDieCountMap())));


        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateDouLuoShrekUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DouLuoShrekUserData parseData(ResultSet rs) throws SQLException {
        DouLuoShrekUserData data = new DouLuoShrekUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setIntegral(rs.getLong("integral"));
        data.setProduceTime(rs.getLong("produceTime"));
        data.setStudentCount(rs.getInt("studentCount"));
        data.setBossId(rs.getInt("bossId"));
        data.setBossBlood(new BigInteger(rs.getString("bossBlood")));
        data.setBossKillTimes(rs.getInt("bossKillTimes"));
        data.setLastEventTime(rs.getLong("eventTime"));
        data.setUpdateTime(rs.getLong("updateTime"));
        String buildingStr  = rs.getString("buildingLevelData");
        if(!StringUtils.isNullOrEmpty(buildingStr)){
            Map<Integer, Integer> result = JSON.parseObject(buildingStr, new TypeReference<ConcurrentHashMap<Integer,Integer>>() {});
            if(result != null){
                data.setBuildingLevelMap(result);
            }
        }
        String buildingLevelUpTimeStr  = rs.getString("buildingLevelUpTimeData");
        if(!StringUtils.isNullOrEmpty(buildingLevelUpTimeStr)){
            Map<Integer, Map<Integer, Long>> result = JSON.parseObject(buildingLevelUpTimeStr, new TypeReference<ConcurrentHashMap<Integer, Map<Integer, Long>>>() {});
            if(result != null){
                data.setBuildingLevelUpTimeMap(result);
            }
        }
        String skillStr  = rs.getString("skillLevelData");
        if(!StringUtils.isNullOrEmpty(skillStr)){
            Map<Integer, Integer> result = JSON.parseObject(skillStr, new TypeReference<ConcurrentHashMap<Integer,Integer>>() {});
            if(result != null){
                data.setSkillLevelMap(result);
            }
        }
        String studentStr  = rs.getString("studentData");
        if(!StringUtils.isNullOrEmpty(studentStr)){
            Map<Integer, DouLuoShrekStudentData> result = JSON.parseObject(studentStr, new TypeReference<ConcurrentHashMap<Integer, DouLuoShrekStudentData>>() {});
            if(result != null){
                data.setStudentMap(result);
            }
        }
        String solderStr  = rs.getString("solderData");
        if(!StringUtils.isNullOrEmpty(solderStr)){
            Map<Integer, String> result = JSON.parseObject(solderStr, new TypeReference<ConcurrentHashMap<Integer, String>>() {});
            if(result != null){
                data.setSolderData(result);
            }
        }
        String eventStr  = rs.getString("eventData");
        if(!StringUtils.isNullOrEmpty(eventStr)){
            Map<Integer, DouLuoShrekEventData> result = JSON.parseObject(eventStr, new TypeReference<ConcurrentHashMap<Integer, DouLuoShrekEventData>>() {});
            if(result != null){
                data.setEventMap(result);
            }
        }
        String titleCountStr  = rs.getString("titleCountData");
        if(!StringUtils.isNullOrEmpty(titleCountStr)){
            Map<Integer, Integer> result = JSON.parseObject(titleCountStr, new TypeReference<ConcurrentHashMap<Integer, Integer>>() {});
            if(result != null){
                data.setSolderTitleCountMap(result);
            }
        }
        String disappearStudentStr  = rs.getString("cacheDisappearStudent");
        if(!StringUtils.isNullOrEmpty(disappearStudentStr)){
            Map<Integer, DouLuoShrekStudentData> result = JSON.parseObject(disappearStudentStr, new TypeReference<ConcurrentHashMap<Integer, DouLuoShrekStudentData>>() {});
            if(result != null){
                data.setCacheDisappearMap(result);
            }
        }
        String dieCountDataStr  = rs.getString("dieCountData");
        if(!StringUtils.isNullOrEmpty(dieCountDataStr)){
            Map<Integer, Integer> result = JSON.parseObject(dieCountDataStr, new TypeReference<ConcurrentHashMap<Integer, Integer>>() {});
            if(result != null){
                data.setDieCountMap(result);
            }
        }
        return data;
    }

}
